package haoyou.csnf.manager.module.pm.service.positionsseq;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import haoyou.csnf.manager.module.pm.controller.admin.positionsseq.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.positionsseq.PositionsSeqDO;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;

import haoyou.csnf.manager.module.pm.dal.mysql.positionsseq.PositionsSeqMapper;

import static haoyou.csnf.manager.framework.common.exception.util.ServiceExceptionUtil.exception;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.convertList;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.diffList;
import static haoyou.csnf.manager.module.pm.enums.ErrorCodeConstants.*;

/**
 * 职位序列 Service 实现类
 *
 * @author 好有经验源码
 */
@Service
@Validated
public class PositionsSeqServiceImpl implements PositionsSeqService {

    @Resource
    private PositionsSeqMapper positionsSeqMapper;

    @Override
    public Long createPositionsSeq(PositionsSeqSaveReqVO createReqVO) {
        // 插入
        PositionsSeqDO positionsSeq = BeanUtils.toBean(createReqVO, PositionsSeqDO.class);
        positionsSeqMapper.insert(positionsSeq);
        // 返回
        return positionsSeq.getId();
    }

    @Override
    public void updatePositionsSeq(PositionsSeqSaveReqVO updateReqVO) {
        // 校验存在
        validatePositionsSeqExists(updateReqVO.getId());
        // 更新
        PositionsSeqDO updateObj = BeanUtils.toBean(updateReqVO, PositionsSeqDO.class);
        positionsSeqMapper.updateById(updateObj);
    }

    @Override
    public void deletePositionsSeq(Long id) {
        // 校验存在
        validatePositionsSeqExists(id);
        // 删除
        positionsSeqMapper.deleteById(id);
    }

    @Override
        public void deletePositionsSeqListByIds(List<Long> ids) {
        // 校验存在
        validatePositionsSeqExists(ids);
        // 删除
        positionsSeqMapper.deleteByIds(ids);
        }

    private void validatePositionsSeqExists(List<Long> ids) {
        List<PositionsSeqDO> list = positionsSeqMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(POSITIONS_SEQ_NOT_EXISTS);
        }
    }

    private void validatePositionsSeqExists(Long id) {
        if (positionsSeqMapper.selectById(id) == null) {
            throw exception(POSITIONS_SEQ_NOT_EXISTS);
        }
    }

    @Override
    public PositionsSeqDO getPositionsSeq(Long id) {
        return positionsSeqMapper.selectById(id);
    }

    @Override
    public PageResult<PositionsSeqDO> getPositionsSeqPage(PositionsSeqPageReqVO pageReqVO) {
        return positionsSeqMapper.selectPage(pageReqVO);
    }

}